define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'lihua_refund/index' + location.search,
                    add_url: 'lihua_refund/add',
                    edit_url: 'lihua_refund/edit',
                    del_url: 'lihua_refund/del',
                    multi_url: 'lihua_refund/multi',
                    table: 'lihua_refund',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'order_sn', title: __('Order_sn') },
                        { field: 'price', title: __('Price'), operate:'BETWEEN', formatter:function(price, value, index){
                            return parseFloat(value.wallet_given_fee) + parseFloat(value.price);
                        } },
                        { field: 'mobile', title: '手机号' },
                        { field: 'pay_type', title: '支付方式', formatter: function(row) {
                                if(row==0){return '——'};
                                if(row==1){return '支付宝'};
                                if(row==2){return '微信'};
                                if(row==3){return '丽华钱包'};
                                if(row==6){return '银联微信'};
                                if(row==7){return '云闪付'};
                            }
                        },
                        { field: 'city_name', title: '城市', operate: false },
                        { field: 'city_id', title: '城市ID', visible: false, operate: false },
                        { field: 'consignee', title: '客户名称' },
                        { field: 'applyreason', title: '申请理由' },
                        { field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime },
                        { field: 'status', title: '退款状态', formatter: function(row) { 
                                if(row==0){return '<span style="color:#f39c12">待处理</span>'};
                                if(row==1){return '<span style="color:#2db907">已退款</span>'};
                                if(row==2){return '<span style="color:#e74c3c">已拒绝</span>'};
                                if(row==3){return '<span style="color:#2db907">已经处理</span>'};
                            }
                        },
                        {
                            field: 'buttons',
                            title: "操作", 
                            events: {
                                'click .not_queryRefund': function (e, value, row, index) {
                                    //默认prompt
                                    layer.prompt({title: '请输入拒绝理由', formType: 2},function(val, index){
                                        $.post("/api/order/refuseapply", { 'sn': row.order_sn, 'refusereason': val }, function(data){
                                            layer.close(index);
                                            if(data.code==0){
                                                layer.msg(data.msg);
                                            }
                                            $(".btn-refresh").trigger("click");
                                        })
                                    });
                                    return false;
                                },
                                'click .queryRefund': function (e, value, row, index) {
                                    //询问框
                                    layer.confirm('你确定要返款给客户吗?', {
                                        btn: ['确定', '取消']
                                    }, function(){
                                        // 返款给客户
                                        // console.log(row.pay_type);
                                        // 这里需要根据不同的支付方式调用不同的退款接口
                                        // 银联微信退款接口
                                        $.post("/api/refund/order_refund",{ "sn" : row.order_sn, "type" : row.pay_type }, function(data){
                                            layer.close(layer.index);
                                            if(data.code==0){
                                                if(data.msg.errMsg=='订单已全额退款'){
                                                    $.post("/api/refund/change_refund_status", {"sn":row.order_sn,"status":1}, function(data){
                                                        $(".btn-refresh").trigger("click");
                                                    })
                                                }else{
                                                    layer.alert('退款失败'+data.msg, {
                                                        title: "退款失败",
                                                        closeBtn: 0
                                                    }, function(index2){
                                                        layer.close(index2);
                                                        $.post("/api/order/refuse_financial", {"sn":row.order_sn,"type":row.pay_type}, function(data){
                                                            layer.msg('处理完毕');
                                                            $(".btn-refresh").trigger("click");
                                                        })
                                                    });
                                                }
                                            }else{
                                                //直接退款
                                                $(".btn-refresh").trigger("click");
                                            }
                                        })
                                    });
                                },
                            }, 
                            formatter : function (e, value, row, index) {
                                if (value.status=="0") {
                                    // return '<span class="btn btn-warning btn-xs btn-info not_queryRefund" style="margin-right:10px">拒绝退款</span>'+
                                    // '<span class="btn btn-xs btn-info queryRefund">确定退款</a>';
                                    
                                    return '<span class="btn btn-error btn-xs btn-info not_queryRefund" style="margin-left: 10px;background: #C00;margin-right: 10px;">拒绝退款</span>'
                                    +'<span class="btn  btn-warning btn-xs btn-info queryRefund">确定退款</span>';

                                } else if (value.status=="1") {
                                    return '<span class="btn btn-xs btn-info btn-success" style="background:#2db907;" data-table-id="table">返款完成</span>';
                                } else if (value.status=="2") {
                                    return '拒绝理由:'+value.refusereason;
                                } else if (value.status=="3") {
                                    return "抄送给财务处理";
                                }
                            }
                        },
                    
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },

        edit: function () {
            Controller.api.bindevent();
        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                //退款操作按钮
                refundbtn: function(value, row, index) {
                    if (row.status=="0") {
                        console.log("row.pay_type::::", row.pay_type);
                        return '<a href="/api/pay/unionpay_wx_queryRefund?sn=' + row.order_sn + '" class="btn btn-xs btn-info btn-ajax" data-table-id="table" data-refresh="1">确定返款</a>';
                    } else {
                        return '<a href="javascript:;" class="btn btn-xs btn-info btn-success" style="background:#2db907;" data-table-id="table" data-refresh="1">返款完成</a>';
                    }
                },
            }
        }

    };
    return Controller;
});